<?php
namespace App\Repositories;

use App\Exceptions\BasicException;

class MerchantRepository
{
    /**
     * 获取支付商户列表
     * @return list
     */
    public function MerchantList($filter = [])
    {
        $where = [];
        if (isset($filter['merchant_type'])) {
            $where[] = ['merchant_type', '=', $filter['merchant_type']];
        }
        if (isset($filter['status'])) {
            $where[] = ['status', '=', $filter['status']];
        }
        if (isset($filter['name'])) {
            $where[] = ['name', 'like', "%" . $filter['name'] . "%"];
        }
        $list = app('model_pay_merchant')->where($where)->select('*')->get()->each(function ($item) {
            $item->add_time = $item->add_time ? date("Y-m-d H:i:s", $item->add_time) : '';
            $item->update_time = $item->update_time ? date("Y-m-d H:i:s", $item->update_time) : '';
        });
        return ['list'=>$list];
    }

    /**
     * 添加支付商户
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function merchantAdd($param=[])
    {
        $result = app('model_pay_merchant')->create($param);
        if($result) {
            return $result;
        }
        throw new BasicException(96001);
    }


    /**
     * 编辑支付商户
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function merchantSave($param=[])
    {
        $rankSave = app('model_pay_merchant')->where('id',$param['id'])->update($param);
        if($rankSave!==false) {
            return true;
        }
        throw new BasicException(96002);
    }

}


?>
